package com.wenheng.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wenheng.client.AuthClient;
import com.wenheng.service.AdminService;
import com.wenheng.vo.entity.EasybuyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: 文恒
 * @Date: 2021/3/17 - 03 - 17 -11:17
 * @Description:com.wenheng.controller
 * @version:1.0
 */
@RestController
public class UmsAdminController {
    @Autowired
    private AuthClient authClient;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private AdminService adminService;
    //用户登录接口
    @PostMapping("/admin/login")
    public Map<String,Object> adminLogin(@RequestBody EasybuyUser easybuyUser, HttpServletRequest request){
        Map<String,Object> mapoutPut = new HashMap<>();
        Map<String,Object> mapplus = authClient.login(easybuyUser.getUsername(),easybuyUser.getPassword());
        String map = JSONObject.toJSONString(mapplus.get("login"));
        EasybuyUser user = JSONObject.parseObject(map,EasybuyUser.class);
        String key = null;
        String uuid = UUID.randomUUID().toString();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        key = simpleDateFormat.format(new Date()) + uuid.substring(0,6) + user.getId() + UUID.randomUUID().toString().substring(0,2);
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(user),7200, TimeUnit.SECONDS);  //存入时间 2分钟
        mapoutPut.put("key",key);
        return mapoutPut;
    }

    //返回出指定用户的信息 token
    @PostMapping("/admin/info")
    public String adminInfo(@RequestParam("token") String token){
        return redisTemplate.opsForValue().get(token);  //取出的token信息
    }
    //返回指定用户的地址信息
    @PostMapping("/admin/address")
    public String adminAddressInfo(@RequestParam(required = false,value = "address")Integer address){
        return JSONObject.toJSONString(adminService.addressInfo(address));
    }
}
